package com.gimi.cloud.bbp.manage.controller.app;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.Page;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.app.ColumnDTO;
import com.gimi.cloud.bbp.entity.app.ColumnEntity;
import com.gimi.cloud.bbp.manage.service.app.ColumnService;

/**
 * @auther loongs
 * @date 2017/12/27  15:28
 */
@RestController
@RequestMapping(value = "column")
public class ColumnController {
    private static Logger logger = LoggerFactory.getLogger(ColumnController.class);

    private ColumnService columnService;

    @Autowired
    public ColumnController(ColumnService columnService) {
        this.columnService = columnService;
    }

    @RequestMapping(value = "/getColumnList")
    public Response getColumnList(@RequestBody ColumnDTO columnDTO, HttpServletResponse response) {
        logger.info("获得栏目列表（分页），入参："+columnDTO);
        Response resp = new Response();
        Page<ColumnDTO> list=columnService.getColumnList(columnDTO);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    @RequestMapping(value = "/addColumn")
    public Response addColumn(@RequestBody ColumnDTO columnDTO, HttpServletResponse response) {
        logger.info("添加栏目，入参："+columnDTO);
        Response resp = new Response();
        boolean result=columnService.addColumn(columnDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.ADD_COLUMN_FAIL);
        }
        return resp;
    }

    @RequestMapping(value = "/editColumn")
    public Response editColumn(@RequestBody ColumnDTO columnDTO, HttpServletResponse response) {
        logger.info("编辑栏目，入参："+columnDTO);
        Response resp = new Response();
        boolean result=columnService.editColumn(columnDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.UPDATE_COLUMN_FAIL);
        }
        return resp;
    }

    @RequestMapping(value = "/gotoEditColumn")
    public Response gotoEditColumn(@RequestBody ColumnDTO columnDTO, HttpServletResponse response) {
        logger.info("转跳到编辑栏目界面，入参："+columnDTO);
        Response resp = new Response();
        ColumnEntity columnEntity=columnService.gotoEditColumn(columnDTO);
        resp.setRes_data(columnEntity);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    @RequestMapping(value = "/deleteColumn")
    public Response deleteColumn(@RequestBody ColumnDTO columnDTO, HttpServletResponse response) throws MsgException {
        logger.info("禁用或者启用栏目，入参："+columnDTO);
        Response resp = new Response();
        boolean result=columnService.deleteColumn(columnDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.DELETE_COLUMN_FAIL);
        }
        return resp;
    }
}
